const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    radio:'2',
    show:false,
    bankRadio:"",
    yhRadio: true,
    bankName:'',
    bankNum:'',
    walletInfo:'',
    payPasswordBox:false,
    passwordVal:'',
    extractNumVal:'',
    bankList:[],//银行列表
    withdrawals:'',//最高提现金额
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // 钱包余额
    this.walletInfo();
  },
  onChange(event){
    this.setData({
      radio: event.detail,
    });
  },

  onClick(event) {
    const { name } = event.currentTarget.dataset;
    this.setData({
      radio: name,
    });
  },

  bankRadioChange(event){
    this.setData({
      bankRadio: event.detail,
    });
  },

  onbankClick(event){
    this.setData({
      bankRadio: event.currentTarget.dataset.name,
      bankNum: event.currentTarget.dataset.banknum,
      show: false
    });

  },


  topUpAmount(e){
    this.setData({
      extractNumVal:e.detail.value
    })
  },

  changeSheet(){
    this.setData({ show: true });
  },

  onClose(){
    this.setData({ show: false});
  },

  addBank(){
    wx.navigateTo({
      url: '/userPage/pages/bindBank/bindBank',
    })
  },



  // 选择银行
  selectBank:function(){
    wx.navigateTo({
      url: '/pages/extractBankList/extractBankList',
    })
  },
  // 提现确定
  extractBtn:function(){
    var { bankName, bankNum, payPasswordBox, extractNumVal } = this.data
    var _this = this
    if (bankNum == ''){
      wx.showToast({
        title: '请选择提现银行',
        icon: 'none',
        duration: 2000
      })
      return false
    }
    if (extractNumVal == '') {
      wx.showToast({
        title: '请输入提现金额',
        icon: 'none',
        duration: 2000
      })
      return false
    }
    this.setData({
      payPasswordBox: !payPasswordBox
    })
  },

  determinePay:function(){
    var {  bankNum, payPasswordBox, passwordVal, extractNumVal } = this.data
    var _this = this
    var url = app.globalData.baseUrl + 'pay/cashOut';
    var data = {
      cash: extractNumVal,
      cashout_type: 2,
      cashout_account: bankNum,
      alternate_password: passwordVal,
      token: app.globalData.token
    }
    wx.showLoading({
      title: '加载中',
    })
    app.wxPostRequest(url, data, (res) => {
      console.log(res)
      wx.hideLoading()
      if (res.code == 1000) {
        wx.showToast({
          title: '提交成功',
          icon: 'success',
          duration: 2000
        })
        setTimeout(function () {
          _this.setData({
            payPasswordBox: !payPasswordBox,
            passwordVal:'',
            extractNumVal:'',
          })
          _this.walletInfo();
        }, 2000)
      }else{
        wx.showToast({
          title: res.message,
          icon: 'none',
          duration: 2000
        })
      }
    }, (err) => {
      wx.hideLoading()
      wx.showToast({
        title: err.data.message,
        icon: 'none',
        duration: 2000
      })
    })
  },
  // 提现金额
  extractNum:function(e){
    this.setData({
      extractNumVal: e.detail.value
    })
  },
  // 支付密码
  payPasswordInp: function (e) {
    this.setData({
      passwordVal: e.detail.value
    })
  },
  // 钱包余额
  walletInfo:function(){
    var _this = this
    var url = app.globalData.baseUrl + 'pay/balance';
    var data = {
      token: app.globalData.token
    }
    wx.showLoading({
      title: '加载中',
    })
    app.wxGetRequest(url, data, (res) => {
      console.log(res)
      wx.hideLoading()
      if (res.code == 1000) {
        let data=res.data.bankList
        _this.setData({
          walletInfo: res.data.balance,
          withdrawals:res.data.withdrawals,
          bankList:data,
          bankRadio: data[0].bank_name,
          bankNum: data[0].bank_account,
        })
      }
    }, (err) => {
      wx.hideLoading()
      wx.showToast({
        title: err.data.message,
        icon: 'none',
        duration: 2000
      })
    })
  },
  // 取消
  cancelPay: function () {
    var { payPasswordBox } = this.data;
    this.setData({
      payPasswordBox: !payPasswordBox
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})
